package admin.models;

import java.util.Collections;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheManager;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyOpts;
import com.googlecode.objectify.ObjectifyService;

import entities.MetaSeo;

public class MetaSeoModel {
	private Objectify ofy;
	
	private Cache cache = null;
	
	private static MetaSeoModel instance = null;
	
	public static MetaSeoModel getInstance() {
		if (instance == null) {
			instance = new MetaSeoModel();
		}
		
		return instance;
	}
	
	private MetaSeoModel() {		
		try {
			ObjectifyService.register(MetaSeo.class);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		try {
            cache = CacheManager.getInstance().getCacheFactory().createCache(Collections.emptyMap());
        } catch (CacheException e) {
           //e.printStackTrace();
        }
		
        ObjectifyOpts opts = new ObjectifyOpts().setSessionCache(true);
		ofy = ObjectifyService.begin(opts);
	}
	
	public MetaSeo get(String id) {
		MetaSeo metaseo = new MetaSeo();
		try {
			metaseo = ofy.get(new Key<MetaSeo>(MetaSeo.class, id));
		} catch (Exception e) {
			metaseo = new MetaSeo();
			metaseo.setId(id);
		}
		
		
		return metaseo;
	}
	
	public void insert(MetaSeo metaseo) {
		ofy.put(metaseo);
	}
	
	public void delete(MetaSeo metaseo) {
		ofy.delete(metaseo);
	}
}
